import sys
from mako.template import Template
from optparse import OptionParser
def main(opts):
    makotext = '''
$ModLoad imtcp
$InputTCPServerRun 514 

$ModLoad omredis
$SystemLogRateLimitInterval 5
$SystemLogRateLimitBurst 500000
$RepeatedMsgReduction off 

$template redis_iptopn,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'iptopn 1 '%msg:F,32+:3:%'"
$template redis_domaintopn,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'domaintopn 1 '%msg:F,32+:8:%'"

$template redis_rtype,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'rtype 1 '%msg:F,32+:10:%'"
$template redis_rcode,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'rcode 1 '%msg:F,32+:11:%'"
$template redis_recursion,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'recursion 1 '%msg:F,32+:12:%'"
$template redis_edns,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'edns 1 '%msg:F,32+:13:%'"
$template redis_sign,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'sign 1 '%msg:F,32+:14:%'"
$template redis_tcp,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'tcp 1 '%msg:F,32+:15:%'"
$template redis_dnssec,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'dnssec 1 '%msg:F,32+:16:%'"
$template redis_cd,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%'cd 1 '%msg:F,32+:13:%'"

$template redis_totalquery,"INCR '%fromhost-ip%:%msg:F,32+:7:%'totalquery"
$template redis_domainip,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%%msg:F,32+:8:%' 1 '%msg:F,32+:3:%'"
$template redis_ipdomain,"ZINCRBY '%fromhost-ip%:%msg:F,32+:7:%%msg:F,32+:3:%' 1 '%msg:F,32+:8%'"

$template myFormat,"%timestamp% %msg%\\n"
% if opts['time'] == 'minute':
$template DynFile,"${opts['log_dir']}/%fromhost-ip%/%$year%%$month%%$day%%$hour%%$minute%.log"
% elif opts['time'] == 'hour':
$template DynFile,"${opts['log_dir']}/%fromhost-ip%/%$year%%$month%%$day%%$hour%.log"
% elif opts['time'] == 'day':
$template DynFile,"${opts['log_dir']}/%fromhost-ip%/%$year%%$month%%$day%.log"
% elif opts['time'] == 'month':
$template DynFile,"${opts['log_dir']}/%fromhost-ip%/%$year%%$month%.log"
% endif

local1.*      :omredis:;redis_domaintopn
local1.*      :omredis:;redis_iptopn
local1.*      :omredis:;redis_rtype
local1.*      :omredis:;redis_rcode
% if 'recursion' in opts and opts['recursion'] == "on":
local1.*      :omredis:;redis_recursion
% endif
% if 'edns' in opts and opts['edns'] == "on":
local1.*      :omredis:;redis_edns
% endif
% if 'sign' in opts and opts['sign'] == "on":
local1.*      :omredis:;redis_sign
% endif
% if 'tcp' in opts and opts['tcp'] == "on":
local1.*      :omredis:;redis_tcp
% endif
% if 'dnssec' in opts and opts['dnssec'] == "on":
local1.*      :omredis:;redis_dnssec
% endif
% if 'cd' in opts and opts['cd'] == "on":
local1.*      :omredis:;redis_cd
% endif

local1.*      :omredis:;redis_totalquery
local1.*      :omredis:;redis_domainip
local1.*      :omredis:;redis_ipdomain

local1.*    ?DynFile;myFormat

*.*           /var/log/messages
'''
    if 'log_dir' not in opts:
        opts['log_dir'] = "/var/log/zddi"
    if 'time' not in opts:
        opts['time'] = "hour"
    tpl = Template(makotext)
    txt = tpl.render(opts = opts)
    print txt
    with open("named_server_log.conf", 'w') as fp:
       fp.write(txt)
def getopt():
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-p", "--path", dest="path", default="./",
        help="specify output path")
    parser.add_option("-t", dest="time", default='hour',
        help="specify time")
    parser.add_option("-d", dest="log_dir", default='/var/log/zddi',
        help="specify log directory")
    parser.add_option("-e", dest="edns", default='off')
    parser.add_option("--tcp", dest="tcp", default='off')
    parser.add_option("--dnssec", dest="dnssec", default='off')
    parser.add_option("--cd", dest="cd", default='off')
    parser.add_option("--sign", dest="sign", default='off')
    parser.add_option("--recursion", dest="recursion", default='off')
    (options, args) = parser.parse_args()

    return options

if __name__ == "__main__":
    opt = getopt()
    main(vars(opt))
